package com.yoomiito.app.ui.my;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scrat.app.selectorlibrary.ImageSelector;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.bean.AddressInfo;
import com.yoomiito.app.model.bean.BankBean;
import com.yoomiito.app.model.my.MyUserInfo;
import com.yoomiito.app.present.my.activity.MyUserInfoPresenter;
import com.yoomiito.app.present.my.activity.RealNamePresenter;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.CheckUtil;
import com.yoomiito.app.utils.Const;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.CircleImageView;
import com.yoomiito.app.widget.ItemView;
import com.yoomiito.app.widget.TipDialog;
import com.yoomiito.app.widget.TitleView;
import com.yoomiito.app.widget.dialog.CustomDialog1;
import com.yoomiito.app.widget.wheelview.DateSelectDialog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.BottomDialog2;
import cn.droidlover.xdroidmvp.cache.SharedPref;

public class RealNameActivity extends BaseActivity<RealNamePresenter> {
    private BottomDialog2 dialog;
    private String mProvince, mProvinceCode, mCity, mCityCode;
    private String mobile;
    private String bankName;
    private int bankId;
    @BindView(R.id.iv_place)
    ItemView place;
    @BindView(R.id.iv_bank)
    EditText branch;

    @BindView(R.id.iv_card)
    EditText ivAccountNumber;

    @BindView(R.id.send_code)
    TextView sendCode;
    @BindView(R.id.iv_bank_name)
    ItemView ivBankName;
    @BindView(R.id.iv_name)
    EditText ivName;

    @BindView(R.id.iv_phone)
    EditText ivPhone;
    @BindView(R.id.iv_id_card)
    EditText ivCard;

    @BindView(R.id.tv_code)
    EditText ivCode;

    private String id;


    @Override
    public int getLayoutId() {
        return R.layout.activity_real_name;
    }

    @Override
    public RealNamePresenter newP() {
        return new RealNamePresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        getP().getBankMessage();
    }


    @OnClick({R.id.iv_name, R.id.iv_place, R.id.iv_bank_name, R.id.tv_save, R.id.send_code,})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_code:
                mobile = ivPhone.getText().toString();
                if (TextUtils.isEmpty(mobile)) {
                    getvDelegate().centerToastShort(getString(R.string.input_phone_num));
                    return;
                }
                if (!CheckUtil.isMobileNO(mobile)) {
                    getvDelegate().centerToastShort(getString(R.string.error_phone_toast));
                    return;
                }
                if (AllUtils.isFastClick()) {
                    return;
                }
                getP().requestSendCode(mobile);

                break;
            case R.id.iv_name:

                break;
            case R.id.iv_place:
                if (dialog == null) {
                    dialog = new BottomDialog2(this);
                    dialog.setOnAddressSelectedListener((province, city, county, street) -> {
                        mProvince = province.getTitle();
                        mProvinceCode = province.getValue();
                        mCity = city.getTitle();
                        mCityCode = city.getValue();
                        place.setRightText(province.getTitle() + city.getTitle());
                        dialog.dismiss();
                    });
                }
                dialog.show();
                break;

            case R.id.iv_bank_name:
                Intent intent = new Intent(this, BankSearchActivity.class);
                startActivityForResult(intent, 1000);

                break;

            case R.id.tv_save:
                String branchName = branch.getText().toString();
                String accountNumber = ivAccountNumber.getText().toString();
                String name = ivName.getText().toString();
                String mobile = ivPhone.getText().toString();
                String idCard = ivCard.getText().toString();
                String code = ivCode.getText().toString();


                if(TextUtils.isEmpty(mProvince)){
                    YMToast.showText("请选择开户所在地");
                    return;
                }
                if(TextUtils.isEmpty(bankName)){
                    YMToast.showText("请选择开户行");
                    return;
                }
                if(TextUtils.isEmpty(branchName)){
                    YMToast.showText("请选择开户支行");
                    return;
                }
                if(TextUtils.isEmpty(accountNumber)){
                    YMToast.showText("请填写银行卡号");
                    return;
                }
                if(TextUtils.isEmpty(name)){
                    YMToast.showText("请填写户名");
                    return;
                }
                if(TextUtils.isEmpty(idCard)){
                    YMToast.showText("请填写身份证号");
                    return;
                }
                if(TextUtils.isEmpty(mobile)){
                    YMToast.showText("请填写预留手机号码");
                    return;
                }
                if(TextUtils.isEmpty(code)){
                    YMToast.showText("请填写验证码");
                    return;
                }
                if (TextUtils.isEmpty(id)) {
                    getP().insertBank(mProvince, mProvinceCode, mCity, mCityCode, bankId, branchName, accountNumber, name,
                            mobile, idCard, code);
                } else {
                    getP().updateBank(id, mProvince, mProvinceCode, mCity, mCityCode, bankId, branchName, accountNumber, name,
                            mobile, idCard, code);
                }

                break;
        }
    }


    public void setData(BankBean data) {

        if (data != null && !TextUtils.isEmpty(data.getId())) {
            id = data.getId();
            bankName = data.getBankName();
            mProvince = data.getProvince();
            mProvinceCode = data.getProvinceCode();
            mCity = data.getCity();
            mCityCode = data.getCityCode();
            bankId = data.getBankId();
            ivBankName.setRightText(data.getBankName());
            branch.setText(data.getBranch());
            ivAccountNumber.setText(data.getAccountNumber());
            ivName.setText(data.getName());
            ivPhone.setText(data.getMobile());
            ivCard.setText(data.getIdCard());
            place.setRightText(mProvince + mCity);
            ivName.setTextColor(Color.parseColor("#666666"));
            ivName.setEnabled(false);
            ivCard.setTextColor(Color.parseColor("#666666"));
            ivCard.setEnabled(false);
        }
    }

    public void CountdownTimeTask(int time) {
        sendCode.setText(time + getString(R.string.send_code_time_hint));
        sendCode.setEnabled(false);
        sendCode.setBackgroundResource(R.drawable.bg_blue1);
    }

    public void timeEndTask() {
        sendCode.setText("重新发送");
        sendCode.setEnabled(true);
        sendCode.setBackgroundResource(R.drawable.bg_blue);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (1000 == requestCode && data != null) {
            bankName = data.getStringExtra("bankName");
            bankId = data.getIntExtra("bankId", 0);
            ivBankName.setRightText(bankName);

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getP().destroyCountdownTask();
    }
}
